// ActionScript file
import com.helloniu.data.wb.WBStyle;
import com.helloniu.fl.util.StyleTool;
import com.helloniu.wb.CursorManager2;
import com.helloniu.wb.containers.BlankPaperWrapper;
import com.helloniu.wb.containers.ShapeTypeDef;
import com.helloniu.wb.tools.DrawingTool;

import mx.core.UIComponent;
import flash.events.Event;
import mx.containers.Canvas;

public var blankPaperWrapper:BlankPaperWrapper;
public var imgCon:Canvas;

public function initApp():void {
    imgSelector.setImageContainer(imgCon);
    imgSelector.setForegroundComponent(blankPaperWrapper);
    wbStyleEditors.setSelectedTool();
}

public function setSelectedItem(dtool:DrawingTool):void {
    wbStyleEditors.setSelectedItem(dtool);
}

public function getWBStyle():WBStyle {
    return wbStyleEditors.getWBStyle();
}

public function setWBStyle(awbstyle:WBStyle):void {
    wbStyleEditors.setWBStyle(awbstyle);
}

private function initApp2():void {
    StyleTool.setBtnStyle(btnNoneTool, "wbNoneIcon");
    StyleTool.setBtnStyle(btnLineTool, "wbLineIcon");
    StyleTool.setBtnStyle(btnPencileTool, "wbPencileIcon");
    StyleTool.setBtnStyle(btnRectTool, "wbRectIcon");
    StyleTool.setBtnStyle(btnRRectTool, "wbRRectIcon");
    StyleTool.setBtnStyle(btnEllipseTool, "wbEllipseIcon");
    StyleTool.setBtnStyle(btnTextTool, "wbTextIcon");
    StyleTool.setBtnStyle(btnLaserTool, "wbLaserIcon");

    StyleTool.setBtnStyle(btnMoveTool, "wbMoveIcon");
    StyleTool.setBtnStyle(btnModifyTool, "wbModifyIcon");
    StyleTool.setBtnStyle(btnRemoveTool, "wbRemoveIcon");

    // pages
    StyleTool.setBtnStyle(btnBeforePage, "beforePageIcon");
    StyleTool.setBtnStyle(btnNextPage, "nextPageIcon");
    StyleTool.setBtnStyle(btnClearPage, "clearPageIcon");
    StyleTool.setBtnStyle(btnCreatePage, "createPageIcon");
    StyleTool.setBtnStyle(btnRemovePage, "removePageIcon");

    // redo,undo
    StyleTool.setBtnStyle(btnUndoTool, "undoIcon");
    StyleTool.setBtnStyle(btnRedoTool, "redoIcon");    
}

private function noneToolClicked(event:Event):void {
    CursorManager2.setToolType(CursorManager2.PAINT_NONE);
    blankPaperWrapper.selectedTool2 = NaN;
    blankPaperWrapper.hideLaser();
    wbStyleEditors.setSelectedTool();
}

private function lineToolClicked(event:Event):void{
    CursorManager2.setToolType(CursorManager2.PAINT_LINE);
    blankPaperWrapper.selectedTool2 = ShapeTypeDef.LINE;
    blankPaperWrapper.hideLaser();
    wbStyleEditors.setSelectedTool(ShapeTypeDef.LINE);
}

private function paintToolClicked(event:Event):void{
    CursorManager2.setToolType(CursorManager2.PAINT_PEN);
    blankPaperWrapper.selectedTool2 = ShapeTypeDef.PENCILE;
    blankPaperWrapper.hideLaser();
    wbStyleEditors.setSelectedTool(ShapeTypeDef.PENCILE);
}

private function rectToolClicked(event:Event):void{
    CursorManager2.setToolType(CursorManager2.PAINT_RECT);
    blankPaperWrapper.selectedTool2 = ShapeTypeDef.RECT;
    blankPaperWrapper.hideLaser();
    wbStyleEditors.setSelectedTool(ShapeTypeDef.RECT);
}

private function rrectToolClicked(event:Event):void{
    CursorManager2.setToolType(CursorManager2.PAINT_RRECT);
    blankPaperWrapper.selectedTool2 = ShapeTypeDef.RRECT;
    blankPaperWrapper.hideLaser();
    wbStyleEditors.setSelectedTool(ShapeTypeDef.RRECT);
}

private function ellipseToolClicked(event:Event):void{
    CursorManager2.setToolType(CursorManager2.PAINT_ELLIPSE);
    blankPaperWrapper.selectedTool2 = ShapeTypeDef.ELLIPSE;
    blankPaperWrapper.hideLaser();

    wbStyleEditors.setSelectedTool(ShapeTypeDef.ELLIPSE);
}

private function textToolClicked(event:Event):void{
    CursorManager2.setToolType(CursorManager2.PAINT_TEXT);
    blankPaperWrapper.selectedTool2 = ShapeTypeDef.TEXT;
    blankPaperWrapper.hideLaser();

    wbStyleEditors.setSelectedTool(ShapeTypeDef.TEXT);
}

private function laserToolClicked(event:Event):void{
    CursorManager2.setToolType(CursorManager2.PAINT_LASER);
    blankPaperWrapper.selectedTool2 = ShapeTypeDef.LASER;
    blankPaperWrapper.showLaser();

    wbStyleEditors.setSelectedTool(ShapeTypeDef.LASER);
}

private function undo(event:Event):void{
    blankPaperWrapper.undo();
}

private function redo(event:Event):void {
    blankPaperWrapper.redo();
}

private function setWBMoveTool():void {
    CursorManager2.setToolType(CursorManager2.MOVE_TOOL);
    blankPaperWrapper.selectedTool2 = NaN;
    blankPaperWrapper.hideLaser();
}

private function setWBModifyTool():void {
    CursorManager2.setToolType(CursorManager2.MODIFY_TOOL);
    blankPaperWrapper.selectedTool2 = NaN;
    blankPaperWrapper.hideLaser();
}

private function deleteComp(event:Event):void  {
    blankPaperWrapper.selectedTool2 = NaN;
    blankPaperWrapper.hideLaser();
    blankPaperWrapper.removeSelectedTool();
}

private var pageNum:Number = 0;
// 翻到前一页
private function beforePage(event:Event):void {
    var num:Number = blankPaperWrapper.wbinfo.getCurrPageNumber();
    pageTxt.text = num + "";

    if(num > 0) {
        pageNum = num - 1;
        gotoPage();
        pageTxt.text = pageNum + "";
    }
}

// 翻到后一页
private function nextPage(event:Event):void {
    var num:Number = blankPaperWrapper.wbinfo.getCurrPageNumber();
    var numtotal:Number = blankPaperWrapper.wbinfo.getPageLength();
    pageTxt.text = num + "";

    if(num < (numtotal - 1)) {
        pageNum = num + 1;
        gotoPage();
        pageTxt.text = pageNum + "";
    }

    //pageNum = parseInt(pageTxt.text);

    //gotoPage();
}

private function gotoPage():void {
    wbStyleEditors.setSelectedTool();

    blankPaperWrapper.getWBInfo().gotoPage(pageNum);
    blankPaperWrapper.showCurrentPage();
}

// 清空某页
private function clearPage(event:Event):void {
	wbStyleEditors.setSelectedTool();
	
    var num:Number = blankPaperWrapper.wbinfo.getCurrPageNumber();

    blankPaperWrapper.getWBInfo().clearPage(num);
    blankPaperWrapper.showCurrentPage();
}

// 创建新页
private function createPage(event:Event):void {
	wbStyleEditors.setSelectedTool();
	
    blankPaperWrapper.getWBInfo().createPage();
    blankPaperWrapper.showCurrentPage();

    var num:Number = blankPaperWrapper.wbinfo.getCurrPageNumber();
    pageTxt.text = num + "";
}

// 删除某页
private function removePage(event:Event):void {
	wbStyleEditors.setSelectedTool();
	
    var numtotal:Number = blankPaperWrapper.wbinfo.getPageLength();
	var num:Number = blankPaperWrapper.wbinfo.getCurrPageNumber();

	if(numtotal == 1) {
		return;
	}
	
    if(num >= numtotal) {
        return;
    }

    blankPaperWrapper.getWBInfo().removePage(num);
    blankPaperWrapper.showCurrentPage();

    num = blankPaperWrapper.wbinfo.getCurrPageNumber();
    pageTxt.text = num + "";
}

private function zoomChanged():void {
	zoomEffct(blankPaperWrapper, zoomSlider.value);
}

// 缩放
private function zoomEffct(comp:UIComponent, zoomNum:Number):void {
	comp.scaleX = zoomNum/100;
	comp.scaleY = zoomNum/100;

	// @by tonyy, force the parent to show the scroll
	// @problem, set scaleX/scaleY is an async function
	comp.width = comp.width;
	comp.height = comp.height;
}